//
//  TimelineTableCell.m
//  aMoment
//
//  Created by dllo on 15/10/7.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "TimelineTableCell.h"
#import "AllHeaderFiles.h"

@implementation TimelineTableCell

- (void)dealloc
{
    [_userImageView release];
    [_usernameLabel release];
    [_addTimeLabel release];
    [_coverImageView release];
//    [_songNameLabel release];
//    [_singerLabel release];
//    [_playImageView release];
    [_leftImageView release];
    [_contentLabel release];
    [_rightImageView release];
    [_commentNumField release];
    [_likeNumField release];
    [_timelineDict release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createViews];
    }
    return self;
}

- (void)createViews
{
    _userImageView = [[UIImageView alloc] init];
    _userImageView.contentMode = UIViewContentModeScaleAspectFill;
    _userImageView.clipsToBounds = YES;
    _userImageView.layer.cornerRadius = IMAGE_32 / 2;
    [self.contentView addSubview:_userImageView];
    [_userImageView release];
    
    _usernameLabel = [[UILabel alloc] init];
    _usernameLabel.font = [UIFont systemFontOfSize:FONTSIZE_MIDDLE];
    [self.contentView addSubview:_usernameLabel];
    [_usernameLabel release];
    
    _addTimeLabel = [[UILabel alloc] init];
    _addTimeLabel.textAlignment = NSTextAlignmentRight;
    _addTimeLabel.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _addTimeLabel.textColor = FONTCOLOR_GRAY;
    [self.contentView addSubview:_addTimeLabel];
    [_addTimeLabel release];
    
    _coverImageView = [[UIImageView alloc] init];
//    _coverImageView.backgroundColor = [UIColor grayColor];
    _coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    _coverImageView.clipsToBounds = YES;
    [self.contentView addSubview:_coverImageView];
    [_coverImageView release];
    
//    _songNameLabel = [[UILabel alloc] init];
//    _songNameLabel.hidden = YES;
//    [self.contentView addSubview:_songNameLabel];
//    [_songNameLabel release];
//    
//    _singerLabel = [[UILabel alloc] init];
//    _singerLabel.hidden = YES;
//    [self.contentView addSubview:_singerLabel];
//    [_singerLabel release];
//    
//    _playImageView = [[UIImageView alloc] init];
//    _playImageView.hidden = YES;
//    [self.contentView addSubview:_playImageView];
//    [_playImageView release];
    
    _leftImageView = [[UIImageView alloc] init];
    _leftImageView.image = [UIImage imageNamed:@"iconfont-markleft"];
    _leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_leftImageView];
    [_leftImageView release];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont systemFontOfSize:FONTSIZE_MIDDLE];
    [self.contentView addSubview:_contentLabel];
    [_contentLabel release];
    
    _rightImageView = [[UIImageView alloc] init];
    _rightImageView.image = [UIImage imageNamed:@"iconfont-markright"];
    _rightImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_rightImageView];
    [_rightImageView release];
    
    _commentNumField = [[UITextField alloc] init];
    _commentNumField.enabled = NO;
    _commentNumField.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _commentNumField.leftView = [self createLeftViewOfTextFieldWithImageName:@"iconfont-message"];
    _commentNumField.leftViewMode = UITextFieldViewModeAlways;
    _commentNumField.textColor = FONTCOLOR_GRAY;
    [self.contentView addSubview:_commentNumField];
    [_commentNumField release];
    
    _likeNumField = [[UITextField alloc] init];
    _likeNumField.enabled = NO;
    _likeNumField.font = [UIFont systemFontOfSize:FONTSIZE_SMALL];
    _likeNumField.leftView = [self createLeftViewOfTextFieldWithImageName:@"iconfont-love"];
    _likeNumField.leftViewMode = UITextFieldViewModeAlways;
    _likeNumField.textColor = FONTCOLOR_GRAY;
    [self.contentView addSubview:_likeNumField];
    [_likeNumField release];
}

#pragma mark 创建textField的leftView
- (UIImageView *)createLeftViewOfTextFieldWithImageName:(NSString *)imageName
{
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, IMAGE_16, IMAGE_16)];
    leftImageView.image = [UIImage imageNamed:imageName];
    return leftImageView;
}

#pragma mark 设置部件的frame
- (void)setFrameOfViewsWithImgHeight:(CGFloat)imgHeight
                   WithContentHeight:(CGFloat)contentHeight
{
//    NSLog(@"%f", self.contentView.Width);
    self.contentView.frame = CGRectMake(0, 0, 375, self.contentView.Height);
    _userImageView.frame = CGRectMake(20 * FIT_WIDTH, 15 * FIT_HEIGHT, 32 * FIT_WIDTH, 32 * FIT_WIDTH);
    _usernameLabel.frame = CGRectMake(_userImageView.X + _userImageView.Width + 5 * FIT_WIDTH, _userImageView.Y + _userImageView.Height / 2 - 10 * FIT_HEIGHT, SCREEN_WIDTH - 40 * FIT_WIDTH - _userImageView.Width - 75 * FIT_WIDTH, 20 * FIT_HEIGHT);
    _addTimeLabel.frame = CGRectMake(_usernameLabel.X + _usernameLabel.Width + 10 * FIT_WIDTH, _usernameLabel.Y, 60 * FIT_WIDTH, _usernameLabel.Height);
    _coverImageView.frame = CGRectMake(_userImageView.X, _userImageView.Y + _userImageView.Height + 15 * FIT_HEIGHT, SCREEN_WIDTH - 40 * FIT_WIDTH, imgHeight);
//    _playImageView.frame = CGRectMake(_coverImageView.X + _coverImageView.Width - 42, _coverImageView.Y + _coverImageView.Height - 42, 32, 32);
//    _singerLabel.frame = CGRectMake(_coverImageView.X, _coverImageView.Y + _coverImageView.Height - 20, _coverImageView.Width - 20 - _playImageView.Width, _addTimeLabel.Height);
//    _songNameLabel.frame = CGRectMake(_singerLabel.X, _singerLabel.Y - 10 - _singerLabel.Height, _singerLabel.Width, _singerLabel.Height);
    
    if (imgHeight < 1) {
        _leftImageView.frame = CGRectMake(_coverImageView.X, _coverImageView.Y + _coverImageView.Height - 5 * FIT_HEIGHT, IMAGE_16, IMAGE_16);
    } else {
        _leftImageView.frame = CGRectMake(_coverImageView.X, _coverImageView.Y + _coverImageView.Height + 10 * FIT_HEIGHT, IMAGE_16, IMAGE_16);
    }
    
    _contentLabel.frame = CGRectMake(_leftImageView.X + _leftImageView.Width + 5 * FIT_WIDTH, _leftImageView.Y + _leftImageView.Height / 2, _coverImageView.Width - _leftImageView.Width * 2 - 10 * FIT_WIDTH, contentHeight);
    
    _rightImageView.frame = CGRectMake(_contentLabel.X + _contentLabel.Width + 5 * FIT_WIDTH, _contentLabel.Y + _contentLabel.Height - _leftImageView.Height / 2, _leftImageView.Width, _leftImageView.Height);
    _likeNumField.frame = CGRectMake(_rightImageView.X + _rightImageView.Width - 60 * FIT_WIDTH, _rightImageView.Y + _rightImageView.Height + 10 * FIT_HEIGHT, 60 * FIT_WIDTH, _addTimeLabel.Height);
    _commentNumField.frame = CGRectMake(_likeNumField.X - 10 * FIT_WIDTH - _likeNumField.Width, _likeNumField.Y, _likeNumField.Width, _likeNumField.Height);
}

#pragma mark 重写set
- (void)setTimelineDict:(NSDictionary *)timelineDict
{
    if (_timelineDict != timelineDict) {
        [_timelineDict release];
        _timelineDict = [timelineDict retain];
    }
    Timeline *timeline = [[Timeline alloc] init];
    timeline = [_timelineDict objectForKey:@"timeline"];
    if (![[[_timelineDict objectForKey:@"timeline"] coverimg] isEqualToString:@""]) {//图片存在
        [self setFrameOfViewsWithImgHeight:[[_timelineDict objectForKey:@"imgHeight"] doubleValue] WithContentHeight:[[_timelineDict objectForKey:@"contentHeight"] doubleValue]];
//        [_coverImageView setImageWithURL:[NSURL URLWithString:timeline.coverimg]];
        [_coverImageView setImageWithURL:[NSURL URLWithString:timeline.coverimg] placeholderImage:[UIImage imageNamed:@"placeholder-vertical"]];
    } else {//图片不存在
        [self setFrameOfViewsWithImgHeight:.0 WithContentHeight:[[_timelineDict objectForKey:@"contentHeight"] doubleValue]];
    }
//    [_userImageView setImageWithURL:[NSURL URLWithString:[timeline.userinfo objectForKey:@"icon"]]];
    [_userImageView setImageWithURL:[NSURL URLWithString:[timeline.userinfo objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:@"iconfont-head@2x"]];
    _usernameLabel.text = [timeline.userinfo objectForKey:@"uname"];
    _addTimeLabel.text = timeline.addtime_f;
    _contentLabel.text = timeline.content;
    
    _commentNumField.text = [NSString stringWithFormat:@" %@", [timeline.counterList objectForKey:@"comment"]];

    _likeNumField.text = [NSString stringWithFormat:@" %@", [timeline.counterList objectForKey:@"like"]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
